package org.glut.backend.controller;

import lombok.extern.slf4j.Slf4j;
import org.glut.backend.domain.PerformanceOrder;
import org.glut.backend.domain.PerformanceOrderResponse;
import org.glut.backend.service.IPerformanceOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单表 前端控制器
 *
 * @author hjd
 * @since 2025-08-06 22:46
 */
@Slf4j
@RestController
@RequestMapping("/public/maoyanmovieproject")
public class PerformanceOrderController {

    @Autowired
    IPerformanceOrderService performanceOrderService;

    /*
     * 查询全部演出订单
     * */
    @GetMapping("/getAllPerformanceOrder")
    public  List<PerformanceOrder>  getAllPerformanceOrder() {
       // log.info("查询演出订单ID：{}", id);
        List<PerformanceOrder> performanceOrderList = this.performanceOrderService.list();
        return performanceOrderList;
    }
    
    /*
     * 添加演出订单
     * */
    @PostMapping("/addPerformanceOrder")
    public ResponseEntity<String> addPerformanceOrder(@RequestBody PerformanceOrder performanceOrder) {
        log.info("添加演出订单：{}", performanceOrder);
        boolean isUpdate = this.performanceOrderService.save(performanceOrder);
        log.info("添加是否成功：{}", isUpdate);
        if (isUpdate) {
            return ResponseEntity.ok("success");
        } else {
            return ResponseEntity.badRequest().body("添加失败");
        }
    }

    /*
     * 删除演出订单
     * */
    @DeleteMapping("/deletePerformanceOrderById/{id}")
    public ResponseEntity<String> deletePerformanceOrder(@PathVariable Integer id) {
        log.info("删除演出订单ID：{}", id);
        boolean isRemove = this.performanceOrderService.removeById(id);
        log.info("删除是否成功：{}", isRemove);
        if (isRemove) {
            return ResponseEntity.ok("success");
        } else {
            return ResponseEntity.badRequest().body("删除失败");
        }
    }

    /*// 根据演出订单ID（orderId）删除相关订单
    @DeleteMapping("/deleteByPerformanceOrderId/{pid}")
    public ResponseEntity<String> deleteByPid(@PathVariable Integer pid) {
        // 构建条件：where pid = ?
        LambdaQueryWrapper<PerformanceOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PerformanceOrder::getOrderId, pid); // 等价于 SQL 的 WHERE pid = pid参数

        // 执行删除（返回值为删除的行数）
        boolean deleteCount = performanceOrderService.remove(queryWrapper);
        if (deleteCount) {
            return ResponseEntity.ok("success");
        } else {
            return ResponseEntity.badRequest().body("删除失败");
        }
    }*/

    /*
     * 根据用户id查询所有演出订单信息和演出名（联合两张表）
     * */
    @GetMapping("selectAllPerformanceOrderByUserId/{id}")
    public List<PerformanceOrderResponse> selectAllPerformanceOrder(@PathVariable Integer id) {
        log.info("查询演出订单ID：{}", id);
        List<PerformanceOrderResponse> performanceOrderList = this.performanceOrderService.selectPerformanceOrderById(id);
        return performanceOrderList;
    }
}
